import { getCanReserveCourse_service, getCrouseInfo_service, getCrouseInfo_service_simple } from "../../graphql/course";
import { useLazyQuery } from "@apollo/client";
import { CourseType } from "../../types/course";

export const useFetch_getCrouseInfo = () => {
  const [get, { loading }] = useLazyQuery(getCrouseInfo_service, {
    fetchPolicy: "network-only"
  });

  const getCourseInfo = async (id: string) => {
    const data = await get({
      variables: {
        id
      }
    });
    return data.data?.getCourseInfo.data as CourseType;
  };

  return [getCourseInfo, loading];
};

export const useFetch_getCrouseInfo_simple = () => {
  const [get, { loading }] = useLazyQuery(getCrouseInfo_service_simple, {
    fetchPolicy: "network-only"
  });

  const getCourseInfo = async (id: string) => {
    const data = await get({
      variables: {
        id
      }
    });
    return data.data?.getCourseInfo.data as CourseType;
  };

  return { getCourseInfo, loading };
};

export const useGetCanReserveCourse = () => {
  const [fun, { loading }] = useLazyQuery(getCanReserveCourse_service, {
    fetchPolicy: "network-only"
  });
  const getCanReserveCourse = async () => {
    const res = await fun();
    return {
      data: res.data.getCanReserveCourse.data as string,
      code: res.data.getCanReserveCourse.code as boolean
    };
  };
  return {
    getCanReserveCourse,
    loading
  };
};
